{% extends "base.html" %}

{% block title %}Book List{% endblock %}

{% load selected %}
{% load humanize %}
{% block content %}
    <form action="{% url list %}" method="get">
        <p id="booksearch">
            <input type="text" class="text" name="filter" value="{{ filter_text }}" />
            {{ form }}
            <select name="field">
                <option {% selected field "any_field" %}>In Any Field</option>
                <option {% selected field "title" %}>Title</option> 
                <option {% selected field "author" %}>Author</option> 
                <option {% selected field "course_code" %}>Course Code</option> 
                {% if user.is_staff %}
                    <option {% selected field "ref" %}>Ref #</option> 
                    <option {% selected field "status" %}>Status</option> 
                    <option {% selected field "user" %}>Student #</option>
                {% endif %}
                <option {% selected field "barcode" %}>Barcode</option>
            </select>
            <input type="submit" class="submit" value="Search" />
        </p>
    </form>
    {% if books.object_list %}
        {% if books.has_other_pages %}
            <div class="PageCounterNav">
                <p>
                {% if books.has_previous %}
                    <a href="?page={{ books.previous_page_number }}&amp;per_page={{ per_page }}&amp;field={{ field }}&amp;filter={{ filter_text }}#pages">
                        <img src="{{ MEDIA_URL }}images/pagenav_prev.gif" alt="Previous Page" />
                    </a>
                {% else %}
                    <img src="{{ MEDIA_URL }}images/pagenav_prev_faded.gif" alt="No Previous Page" />
                {% endif %}
                <a name="pages">Pages</a>: ({{ books.number }} of {{books.paginator.num_pages}})
                {% if books.has_next %}
                    <a href="?page={{ books.next_page_number }}&amp;per_page={{ per_page }}&amp;field={{ field }}&amp;filter={{ filter_text }}#pages">
                        <img src="{{ MEDIA_URL }}images/pagenav_next.gif" alt="Next Page" />
                    </a>
                {% else %}
                    <img src="{{ MEDIA_URL }}images/pagenav_next_faded.gif" alt="No Next Page" />
                {% endif %}
            </div>
        {% endif %}
        <form action="{% url update_book %}" method="post" name="ListItems">
        <table cellspacing='0'>
            <thead>
                <tr>
                    <th>
                        <a href="javascript:void(0);" onclick="toggleCBTogether();">All</a>
                    </th>
                    <th>
                        <a href="{{ cube.books.views.list }}?page={{ page }}&amp;per_page={{ per_page }}&amp;sort_by=metabook__title&amp;dir={{ dir }}">Title</a>
                    </th>
                    <th>
                        <a href="{{ cube.books.views.list }}?page={{ page }}&amp;per_page={{ per_page }}&amp;sort_by=metabook__author&amp;dir={{ dir }}">Author</a>
                    </th>
                    <th>
                        <a href="{{ cube.books.views.list }}?page={{ page }}&amp;per_page={{ per_page }}&amp;sort_by=price&amp;dir={{ dir }}">Price</a>
                    </th>
                    <th>
                        <a href="{{ cube.books.views.list }}?page={{ page }}&amp;per_page={{ per_page }}&amp;sort_by=metabook__courses&amp;dir={{ dir }}">Course Code</a>
                    </th>
                    {% if user.is_staff %}
                    <th>
                        <a href="{{ cube.books.views.list }}?page={{ page }}&amp;per_page={{ per_page }}&amp;sort_by=id&amp;dir={{ dir }}">Ref#</a>
                    </th>
                    <th>
                        <a href="{{ cube.books.views.list }}?page={{ page }}&amp;per_page={{ per_page }}&amp;sort_by=status&amp;dir={{ dir }}">Status</a>
                    </th>
                    {% endif %}
                </tr>
            </thead>
            <tbody>
            {% for book in books.object_list %}
                <tr class="{% cycle 'bgcolor_odd' 'bgcolor_even' %}">
                    <td class='row_check'>
                        <input type='checkbox' name="idToEdit{{ forloop.counter }}" value="{{ book.id }}" />
                    </td>
                    <td class='row_title'>
                        {{ book.metabook.title }}
                        {% ifnotequal book.metabook.edition 1 %}
                            ({{ book.metabook.edition|ordinal }} ed.)
                        {% endifnotequal %}
                    </td>
                    <td class="row_author">
                        {{ book.metabook.author }}
                    </td>
                    <td class="row_price">
                        &#36;{{ book.price|floatformat:2 }}
                    </td>
                    <td class="row_coursecode">
                        {{ book.metabook.course_codes }}
                    </td>
                    {% if user.is_staff %}
                    <td class="row_refnum">
                        {% if user.is_staff %}
                        <a href="{% url book book.id %}">
                        {% endif %}
                        {{ book.id }}
                        {% if user.is_staff %}</a>{% endif %}
                    </td>
                    <td class="row_status">
                        {{ book.get_status_display }}
                    </td>
                    {% endif %}
                </tr>
            {% endfor %}
            </tbody>
        </table>
        <p class="submit_options">
        {% if user.is_staff %}
            <input type="submit" name="Action" value="Sold" />
            <input type="submit" name="Action" value="Missing" />
            <input type="submit" name="Action" value="Seller Paid" />
        {% endif %}
            <input type="submit" name="Action" value="Place on Hold" />
        {% if user.is_staff %}
            <input type="submit" name="Action" value="Remove Holds" />
            <input type="submit" name="Action" value="Edit" />
            <input type="submit" name="Action" value="To Be Deleted" />
            <input type="submit" name="Action" value="Delete" />
        {% endif %}
        </p>
        </form>
    {% else %}
        <p class='noresults'>
        {% if filter_text %}
            We could not find any results for "{{ filter_text }}".
        {% else %}
            There are no books available at this time.
        {% endif %}
        </p>
    {% endif %}
{% endblock %}
